package fr.pud.client.view;

import java.awt.Color;
import java.awt.Graphics;
import java.util.Collection;
import java.util.HashSet;
import java.util.Observable;
import java.util.Observer;

import fr.pud.client.model.Queries;
import fr.pud.client.model.Query;
import fr.pud.utils.Tools;

/**
 * Apr 11, 2012 - Projet_Ose.
 * @author Simon Devineau, Adrien Hierry, Leopold Houdin, Pierre Reliquet
 *         Ecole des Mines de Nantes
 *         MapQueries.java
 *         A MapQueries is the graphic representation of a queries.
 *         It is a list of MapQuery draw on the map.
 */
public class MapQueries implements Observer {
    /**
     * @return the instance
     */
    public static MapQueries getInstance() {
        initialize();
        return instance;
    }

    private static void initialize() {
        if (instance == null) {
            instance = new MapQueries();
        }
    }

    private Collection<MapQuery> queries;
    private static MapQueries    instance;
    /**
     * The boolean which specified that the MapQueries are visible or not.
     */
    private static boolean       isVisible = true;

    /**
     * Initialize the list of MapQuery.
     */
    public MapQueries() {
        this.queries = new HashSet<MapQuery>();
    }

    /**
     * @return the queries
     */
    public Collection<MapQuery> getQueries() {
        return this.queries;
    }

    /**
     * @return the visibility of the MapQueries.
     */
    public boolean isMapQueriesVisible() {
        return isVisible;
    }

    /**
     * Paint the list of MapQuery. on the map.
     * @param g
     *            , the graphic where the queries will be displayed.
     */
    public void paint(Graphics g) {
        int index = 0;
        Color c;
        for (MapQuery q : this.queries) {
            index = (index + 1) % Tools.COLORS.length;
            c = Tools.COLORS[index];
            q.paint(g, q.getPickup(), q.getDelivery(), c);
            if (!q.getDelivery().isVisible()) {
                q.getDelivery().setVisible(true);
            }
            if (!q.getPickup().isVisible()) {
                q.getPickup().setVisible(true);
            }
        }
    }

    /**
     * Change the visibility of the MapQueries.
     * @param isVisible
     *            , if true the MapQueries are visible.
     */
    public void setMapQueriesVisible(boolean isVisible) {
        MapQueries.isVisible = isVisible;
        JMapViewerExtended.getJMapViewer().repaint();
    }

    /**
     * @param aQueries
     *            the queries to set
     */
    public void setQueries(Collection<MapQuery> aQueries) {
        initialize();
        this.queries = aQueries;
    }

    /**
     * If a query was added to the model, a MapQuery (based on this query) is
     * added to the view MapQueries. Then the method pain is called.
     */
    @Override
    public void update(Observable aO, Object aArg) {
        if (aArg instanceof Query) {
            ((HashSet<MapQuery>) this.queries).add(new MapQuery((Query) aArg));
            MapQueries.getInstance().paint(
                    JMapViewerExtended.getJMapViewer().getGraphics());
        }
        else {
            for (MapQuery mapQ : this.queries) {
                mapQ.getDelivery().getPointer().removeAll();
                mapQ.getPickup().getPointer().removeAll();
            }
            for (Query q : Queries.getInstance()) {
                ((HashSet<MapQuery>) this.queries).add(new MapQuery(q));
            }
            MapQueries.getInstance().paint(
                    JMapViewerExtended.getJMapViewer().getGraphics());
        }
    }
}
